package com.brigadeba.framework.service;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.brigadeba.framework.dao.PlayerHibernateDAOHibernate;
import com.brigadeba.framework.dao.TeamHibernateDAOHibernate;
import com.brigadeba.framework.model.*;

public class TeamManager {

	@Autowired
	private TeamHibernateDAOHibernate teamDAO;
	
	@Autowired
	private PlayerHibernateDAOHibernate playerDAO;
	
	public void printTeamList(){
		List<Team> list = teamDAO.listTeam();
		for(Team t:list){
			System.out.println(t.toString());
		}
	}
	
	public void printPlayerList(){
		List<Player> list = playerDAO.listPlayer();
		for(Player p:list){
			System.out.println(p.toString());
		}
	}
	
	public Team createTeam(String name){
		Team t = new Team();
		t.setName(name);
		teamDAO.saveTeam(t);
		return t;
	}
	
	public Player addPlayer(String name, String email, int team_id){
		Team t = teamDAO.getTeam(team_id);
		Player p = new Player();
		p.setName(name);
		p.setEmail(email);
		p.setTeam(t);
		
		Set<Player> list = t.getPlayers();
		list.add(p);
		t.setPlayers(list);
		
		return p;
	}
	
	public TeamHibernateDAOHibernate getTeamDAO() {
		return teamDAO;
	}
	public void setTeamDAO(TeamHibernateDAOHibernate teamDAO) {
		this.teamDAO = teamDAO;
	}
	public PlayerHibernateDAOHibernate getPlayerDAO() {
		return playerDAO;
	}
	public void setPlayerDAO(PlayerHibernateDAOHibernate playerDAO) {
		this.playerDAO = playerDAO;
	}
}
